package com.jiqiu.controller;


import com.jiqiu.controller.vo.LoginVo;
import com.jiqiu.entity.Admin;
import com.jiqiu.pojo.Result;
import com.jiqiu.service.IAdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Pattern;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author jiqiu
 * @since 2024-07-30
 */
@Tag(name = "管理员接口")
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
@Validated
public class AdminController {
    // 构造注入
    private final IAdminService iAdminService;

    @Operation(summary = "登录接口")
    @PostMapping("/login")
    public Result<LoginVo> login(
            @Pattern(regexp = "^\\S{5,15}$")
            String username,
            @Pattern(regexp = "^\\S{5,15}$")
            String password) {
        Admin admin = iAdminService.login(username, password);
        LoginVo loginVo = new LoginVo();
        BeanUtils.copyProperties(admin, loginVo);
        return Result.success(loginVo, "登录成功");

    }

    @Operation(summary = "注册接口")
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,15}$") String username, @Pattern(regexp = "^\\S{5,15}$") String password) {
        iAdminService.register(username, password);
        return Result.success("注册成功");
    }

}
